Repository navigation
Initialize empty std::string values with a null terminator - #478
Merged
Merged
Conversation
Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix incorrect std::string rule initialization
Initialize empty Oct 7, 2026
std::string values with a null terminator
nunoplopes
marked this pull request as ready for review
October 7, 2026 18:48
joaotgouveia
pushed a commit
to joaotgouveia/cpp2rust
that referenced
this pull request
Oct 27, 2026
…t#478) The `std::string` rules model strings as null-terminated vectors, but default initialization produced an empty vector. Operations such as `length()` then incorrectly assume a terminator exists. - **Type rules:** Initialize default strings with `vec![0]` in both unsafe and refcount models. - **Regression coverage:** Add a default-constructed string case that checks `length()`. ```cpp std::string empty; assert(empty.length() == 0); ``` <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes Cpp2Rust#455 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Nuno Lopes <nuno.lopes@tecnico.ulisboa.pt>
nunoplopes
pushed a commit
that referenced
this pull request
Nov 15, 2026
String take expansions in both models now leave `vec![0]`, reusing the default initializer from #478. Previously, `std::mem::take` left no terminator: `size()` wrapped and the unsafe substring length could underflow. C++ leaves a moved-from string valid with an unspecified value; choosing empty matches libstdc++ and libc++. Implicit returns of locals and by-value string parameters keep `take`, avoiding a replacement allocation; destructors inspecting that returned local or parameter still see the old empty vector. Returns that move from reference parameters still reset the source. Explicit `return std::move(x)` still resets x and pays the allocation, because a local destructor can observe x after an explicit move. **Cost:** each reset allocates a one-byte buffer. Generated memberwise move constructors for user records also pay this per moved string field, including when returning a record. The cost is not confined to move-only loops. **Remaining:** moves made by the runtime's generic move remain unfixed: `std::pair<std::string,T>` (including moved-pair `push_back`), `std::array<std::string,N>`, and `std::map::operator[]` with a moved key. `tests/unit/string_moved_from.cpp` checks size, substring, terminators, moved-to payloads and return paths against native C++. [Case and evidence](<https://app.perfloop.ai/t/oss/case_pqkwkc8aqd>) --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). --------- Signed-off-by: Tomás Senart <ts@perfloop.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
std::stringrules model strings as null-terminated vectors, but default initialization produced an empty vector. Operations such aslength()then incorrectly assume a terminator exists.vec![0]in both unsafe and refcount models.length().std::stringrule initialization #455